//
//  ViewController.m
//  MvvmDemo
//
//  Created by HarrisonXi on 2018/11/27.
//  Copyright © 2018 harrisonxi.com. All rights reserved.
//

#import "MVVMViewController.h"
#import "CommonHeader.h"
#import "MVVMViewModel.h"

#import "MVVMLoginView.h"
#import "MVVMLoginModel.h"

@interface MVVMViewController ()

@property (nonatomic, strong) MVVMLoginView *loginView;
@property (nonatomic, strong) MVVMLoginModel *loginModel;

@property (nonatomic, strong) MVVMViewModel *viewModel;

@end

@implementation MVVMViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //模型
    self.loginModel = [[MVVMLoginModel alloc] init];
    
    //视图
    self.loginView = [[MVVMLoginView alloc] initWithFrame:CGRectMake(0, 50, [UIScreen mainScreen].bounds.size.width, 400)];
    [self.view addSubview:self.loginView];
    
    [self.loginView.loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    //按钮先不可点击
    self.loginView.usernameTextField.backgroundColor = WhiteBgColor;
    self.loginView.passwordTextField.backgroundColor = WhiteBgColor;
    self.loginView.loginButton.enabled = NO;
    
    //ViewModel
    self.viewModel = [MVVMViewModel new];
    self.viewModel.loginModel = self.loginModel;
    
    // bind input signals
    //这绑定的是控制器持有的model
    RAC(self.viewModel, self.loginModel.userName) = self.loginView.usernameTextField.rac_textSignal;
    RAC(self.viewModel, self.loginModel.password) = self.loginView.passwordTextField.rac_textSignal;
    // bind output signals
    //这里绑定的是控制器持有的view
    RAC(self.loginView.usernameTextField, backgroundColor) = ConvertInputStateToColor(RACObserve(self.viewModel, usernameInputState));
    RAC(self.loginView.passwordTextField, backgroundColor) = ConvertInputStateToColor(RACObserve(self.viewModel, passwordInputState));
    RAC(self.loginView.loginButton, enabled) = RACObserve(self.viewModel, loginEnabled);
}

- (void)loginButtonClick{
    NSLog(@"登录按钮点击了");
    
    NSLog(@"userName = %@  password = %@", self.loginModel.userName, self.loginModel.password);
}

@end
